package com.my.xq.repository;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.my.xq.domain.PlanQueryParam;
import com.my.xq.domain.entity.Plan;
import com.sskj.core.query.annotation.DynamicQuery;
import com.sskj.core.repository.GenericRepository;

public interface PlanRepository extends GenericRepository<Plan, String> {
	@DynamicQuery
	Page<Plan> findPage(PlanQueryParam pqp,Pageable pageable);
	
	@Transactional
	@Modifying(clearAutomatically = true)
	@Query("UPDATE Plan x SET x.planState = ?1 WHERE x.id=?2")
	void changePlanState(int planState,String planId);
	
	@Query("SELECT r FROM Plan r " + " WHERE r.id IN ("
			+ "SELECT x.planid FROM Plan_Court x where x.courtid=?1)"
			+ "AND r.planState=3")
	List<Plan> findPlanInProcess(String courtId);
	
	@Query("SELECT r FROM Plan r " + " WHERE r.id IN ("
			+ "SELECT x.planid FROM Plan_Court x where x.courtid IN ("
			+ "SELECT g.courtId FROM Doorplate g where g.proprietorId = ?1))"
			+ "AND r.planState = ?2")
	List<Plan> findPlanByProprietor(String proprietorId,int planState);
}
